package com.fivefu.core.hillstone.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hillstone.entity.DbHillstoneOrganization;
import com.fivefu.core.hillstone.mapper.DbHillstoneOrganizationMapper;
import com.fivefu.core.hillstone.services.DbHillstoneOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 大华组织表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-18 14:28:12
 */
@Service
public class DbHillstoneOrganizationServiceImpl extends ServiceImpl<DbHillstoneOrganizationMapper, DbHillstoneOrganization> implements DbHillstoneOrganizationService {

    @Autowired
    private DbSysDictService dbSysDictService;
    /**
     * 通过唯一标识查询组织信息
     * @param orgCode
     * @return
     */
    public DbHillstoneOrganization queryHillstoneOrganization(String orgCode){
        QueryWrapper<DbHillstoneOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("org_code",orgCode);
        DbHillstoneOrganization org = baseMapper.selectOne(wrapper);
        return org;
    }

    /**
     * 编辑组织信息
     * @param org
     */
    @Override
    public void editHillstoneOrganization(DbHillstoneOrganization org){
        LocalDateTime time = LocalDateTime.now();
        DbHillstoneOrganization oldOrg = queryHillstoneOrganization(org.getOrgCode());
        org.setUpdatedBy("sys");
        org.setUpdatedTime(time);

        //转存数据字典的组织类型
        if (StrUtils.isNotNull(org.getOrgType())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("CT", String.valueOf(Integer.parseInt(org.getOrgType()) + 2));
            if (null != dict) {
                org.setOrgType(dict.getDictCode());
            }
        }
        if (oldOrg != null){
            org.setId(oldOrg.getId());
            baseMapper.updateById(org);
        }else{
            org.setCreatedBy("sys");
            org.setCreatedTime(time);
            org.setIsDelete(0);
            baseMapper.insert(org);
        }
    }

    /**
     * 查询组织表所有数据
     * @return
     */
    @Override
    public List<DbHillstoneOrganization> queryAll() {
        QueryWrapper<DbHillstoneOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbHillstoneOrganization> organizationList = baseMapper.selectList(wrapper);
        return organizationList;
    }

    /**
     * 删除本地多余组织数据
     * @param orgCode    组织唯一编码
     * @return
     */
    @Override
    public void deleteRedundantData(String orgCode) {
        LocalDateTime time = LocalDateTime.now();
        List<DbHillstoneOrganization> organizationList = queryOrganizationList(orgCode);

        //判断是否已经存在相关数据信息
        if (null != organizationList && organizationList.size() > 0) {
            List<Long> idList = organizationList.stream().map(k -> k.getId()).collect(Collectors.toList());
            Map<String, Long> orgMap = organizationList.stream().collect(Collectors.toMap(k -> k.getOrgCode(), v -> v.getId()));
            if (StrUtils.isNotNull(orgCode)) {
                if (orgMap.containsKey(orgCode)) {
                    Long id = orgMap.get(orgCode);
                    idList.remove(id);
                }
            }

            //删除多余组织数据
            if (idList.size() > 0 ) {
                DbHillstoneOrganization org = new DbHillstoneOrganization();
                org.setIsDelete(1);
                org.setUpdatedBy("sys");
                org.setUpdatedTime(time);

                QueryWrapper<DbHillstoneOrganization> wrapper = new QueryWrapper<>();
                wrapper.in("id",idList);
                wrapper.eq("org_code",orgCode);
                wrapper.eq("is_delete",0);
                baseMapper.update(org,wrapper);
            }
        }
    }

    /**
     * 根据组织唯一编码查询相关数据集
     * @param orgCode
     * @return
     */
    @Override
    public List<DbHillstoneOrganization> queryOrganizationList(String orgCode) {
        QueryWrapper<DbHillstoneOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("org_code",orgCode);
        List<DbHillstoneOrganization> organizationList = baseMapper.selectList(wrapper);
        return organizationList;
    }
}
